Imports System.Data.SqlClient

Friend Class DBConfig
    Inherits DBBase

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Load(ByRef oObject As Config)
        Dim sSQL As String = "select"
        sSQL = sSQL & "     [Name],"
        sSQL = sSQL & "     [Value]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     [Configuration]"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        If oData.Rows.Count > 0 Then
            With oObject
                .Name = oData.Rows(0).Item("Name")
                .Value = oData.Rows(0).Item("Value")
            End With
        Else
            oObject.ObjectExists = False
        End If

        oData.Dispose()
    End Sub

    Public Sub Update(ByRef oObject As Config)
        Dim sSQL As String = "update [Configuration] set"
        sSQL = sSQL & "     [Name]=" & ValueToDBValue(oObject.Name) & ","
        sSQL = sSQL & "     [Value]=" & ValueToDBValue(oObject.Value)
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub

    Public Sub Add(ByRef oObject As Config)
        Dim bIsSaved As Boolean = False

        Do Until bIsSaved = True
            Dim oNextID As New DBNextIDValue("App_Config")
            Dim lNextID As Long = oNextID.NextValue
            oObject.ID = lNextID

            Dim sSQL As String = "insert into [Configuration] ("
            sSQL = sSQL & "     [ID],"
            sSQL = sSQL & "     [Name],"
            sSQL = sSQL & "     [Value]) values ("

            sSQL = sSQL & ValueToDBValue(oObject.ID) & ","
            sSQL = sSQL & ValueToDBValue(oObject.Name) & ","
            sSQL = sSQL & ValueToDBValue(oObject.Value)
            sSQL = sSQL & ")"

            Try
                MyBase.ExecuteNonQuery(sSQL)

                bIsSaved = True

            Catch oEX As System.Data.SqlClient.SqlException
                If InStr(oEX.Message, "Violation of PRIMARY KEY constraint") > 0 Then
                    '---This ID is already in use.
                Else
                    Throw oEX
                End If
            End Try
        Loop
    End Sub

    Public Sub Delete(ByRef oObject As Config)
        Dim sSQL As String = "delete from [Configuration]"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub
End Class
